(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.jobParametersController', jobParametersController);
	jobParametersController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService'];
	function jobParametersController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("jobParametersController");
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.parameters = [];
		
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok']() !== false) {
				$uibModalInstance.close();
			}
		};
		
		function buildParameterXml (parameters){
			 var parser = new DOMParser();
			 var xmlDoc = parser.parseFromString("<params></params>", 'text/xml');
			 var root = xmlDoc.documentElement;
			 for(var i=0 ; i<parameters.length ; i++)
	 		 {
	 		 	
				var newNode = xmlDoc.createElement("param");
				
				newNode.setAttribute("name",parameters[i].name);
			    if(parameters[i].type)	
					newNode.setAttribute("type",parameters[i].type);
				if(parameters[i].format)    
					newNode.setAttribute("format",parameters[i].format);	
				var nodeValue = xmlDoc.createTextNode(parameters[i].value);	
				newNode.appendChild(nodeValue);		
			    root.appendChild(newNode);
	 		 }
			 ctrl.parametersXmlStr = new XMLSerializer().serializeToString(xmlDoc);
		};
		
		ctrl.close = function() {
			buildParameterXml(ctrl.parameters);
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close'](ctrl.parametersXmlStr) !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.jobParameters.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.parameters';
			
			ctrl.typeOptions = {
					"data" : [ {
						"value" : "",
						"label" : ""
					}, {
						"value" : "date",
						"label" : "date"
					}, {
						"value" : "timestamp",
						"label" : "timestamp"
					}, {
						"value" : "time",
						"label" : "time"
					}  ]
			};
			ctrl.formatOptions = {
					"data" : [ {
						"value" : "",
						"label" : ""
					}, {
						"value" : "Date",
						"label" : "Date"
					}, {
						"value" : "SQL Date",
						"label" : "SQL Date"
					}, {
						"value" : "Timestamp",
						"label" : "Timestamp"
					} , {
						"value" : "Time",
						"label" : "Time"
					} , {
						"value" : "String",
						"label" : "String"
					} , {
						"value" : "Boolean",
						"label" : "Boolean"
					} , {
						"value" : "Double",
						"label" : "Double"
					} , {
						"value" : "Float",
						"label" : "Float"
					} , {
						"value" : "Long",
						"label" : "Long"
					} , {
						"value" : "Integer",
						"label" : "Integer"
					} , {
						"value" : "Short",
						"label" : "Short"
					} , {
						"value" : "Byte",
						"label" : "Byte"
					} ]
			};
		};

		ctrl.addParameter = function() {
			ctrl.parameters.push({
			});
		};

		ctrl.removeParameter = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			angular.forEach(ctrl.selectedRows, function(selectedRow, key) {
				angular.forEach(ctrl.parameters, function(parameter, index) {
					if (parameter.$$hashKey == selectedRow.$$hashKey) {
						ctrl.parameters.splice(index, 1);
					}
				});
			});
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);